package com.example.demo.Controller;

import com.example.demo.Bean.*;
import com.example.demo.Service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class AdminController {
    @Autowired
    private AdminService adminService;

    @PostMapping("/selectAdmin")
    public Boolean selectAdmin(String username,String password){
        Admin admin = adminService.selectAdmin(username);
        try{
            String username1 = admin.getUsername();
            String password1 = admin.getPassword();
            String md5 = username + password;
            byte[] bytes = md5.getBytes();
            String pwd = DigestUtils.md5DigestAsHex(bytes);
            if (username1.equals(username) && pwd.equals(password1)){
                return true;
            }
        }catch (Exception e){
            return false;
        }
        return false;
    }

    @PostMapping("/insertAdmin")
    public Boolean insertAdmin(String username,String password){
        Admin admin = adminService.selectAdmin(username);
        try{
            admin.getUsername();
            return false;
        }catch (Exception e){
            String md5 = username + password;
            byte[] bytes = md5.getBytes();
            String pwd = DigestUtils.md5DigestAsHex(bytes);
            Admin admin1 = new Admin();
            admin1.setUsername(username);
            admin1.setPassword(pwd);
            adminService.insertAdmin(admin1);
            int adminId = adminService.getAdminId(username);
            adminService.insertRole(adminId);
            return true;
        }
    }

    @PostMapping("/getRole")
    public Role getRole(String username){
        return adminService.selectRole(username);
    }

    @GetMapping("/getAllProduct")
    public List<AdminProducts> getAllProduct(){
        return adminService.selectAllProduct();
    }

    @PostMapping("/updateProduct")
    public Boolean updateProduct(Integer id,String category,String title,Double price,Integer stock,String status){
        try{
            UpdateProduct updateProduct = new UpdateProduct();
            updateProduct.setId(id);
            updateProduct.setCategory(category);
            updateProduct.setTitle(title);
            updateProduct.setPrice(price);
            updateProduct.setStock(stock);
            updateProduct.setStatus(status);
            adminService.updateProduct(updateProduct);
            return true;
        }catch (Exception e){
            return false;
        }

    }

    @GetMapping("/getAllCheck")
    public List<AdminProducts> getAllCheck(){
        return adminService.selectAllCheck();
    }

    @PostMapping("/updateStatus")
    private Boolean updateStatus(Integer id,String category,String status){
        try{
            UpdateParameter updateParameter = new UpdateParameter();
            updateParameter.setId(id);
            updateParameter.setCategory(category);
            updateParameter.setStatus(status);
            adminService.updateStatus(updateParameter);
            return true;
        }catch (Exception e){
            return false;
        }
    }

    @GetMapping("/getAllUser")
    public List<AllUser> getAllUser(){
        return adminService.getAllUser();
    }

    @GetMapping("/getAllShop")
    public List<AllShop> getAllShop(){
        return adminService.getAllShop();
    }

    @GetMapping("/getAllAdmin")
    public List<AllAdmin> getAllAdmin(){
        return adminService.getAllAdmin();
    }

    @PostMapping("/updateRole")
    private Boolean updateRole(RoleChange roleChange){
        try{
            adminService.updateRole(roleChange);
            return true;
        }catch (Exception e){
            return false;
        }
    }

    @GetMapping("/getCheckCoupon")
    public List<Coupon> getCheckCoupon(){
        return adminService.checkCoupon();
    }

    @PostMapping("/updateCouponStatus")
    public Boolean updateCouponStatus(UpdateCouponParameter updateCouponParameter){
        try{
            adminService.updataCouponStatus(updateCouponParameter);
            return true;
        }catch (Exception e){
            return false;
        }
    }

    @GetMapping("/getCheckShop")
    public List<AllShop> getCheckShop(){
        return adminService.getCheckShop();
    }

    @PostMapping("/updateShopStatus")
    public Boolean updateShopStatus(UpdateStatusParameter updateStatusParameter){
        try{
            adminService.updateShopStatus(updateStatusParameter);
            return true;
        }catch (Exception e){
            return false;
        }
    }

    @GetMapping("/getTopSellProduct")
    public List<AdminProducts> getTopSellProduct(){
        return adminService.getTopSellProduct();
    }

    @GetMapping("/getAllIndexProducts")
    public List<IndexProducts> getAllIndexProducts(){
        return adminService.getIndexProduct();
    }

    @PostMapping("/changeIndexProduct")
    public Boolean changeIndexProduct(Integer id,String category,Integer lastid){
        try{
            CommentParameter commentParameter = new CommentParameter();
            commentParameter.setId(id);
            commentParameter.setCategory(category);
            ChangIndexParameter changIndexParameter = adminService.getChangIndexParameter(commentParameter);
            String title = changIndexParameter.getTitle();
            Double price = changIndexParameter.getPrice();
            Integer buyer = changIndexParameter.getBuyer();
            IndexProducts indexProducts = new IndexProducts();
            indexProducts.setId(lastid);
            indexProducts.setTitle(title);
            indexProducts.setPrice(price);
            indexProducts.setBuyer(buyer);
            indexProducts.setProduct_id(id);
            indexProducts.setCategory(category);
            adminService.updateIndexProduct(indexProducts);
            return true;
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
    }

    @GetMapping("/getAllPaySuccessOrders")
    public List<PaySuccessOrders> getAllPaySuccessOrders(){
        return adminService.getAllPaySuccessOrders();
    }

    @GetMapping("/getAllPaySuccessOrdersByShopName")
    public List<PaySuccessOrders> getAllPaySuccessOrdersByShopName(String shopname){
        return adminService.getAllPaySuccessOrdersByShopName(shopname);
    }

    @GetMapping("/getAllSeckillToCheck")
    public List<CheckSeckill> getAllSeckillToCheck(){
        return adminService.getAllSeckill();
    }

    @PostMapping("/updateSeckillStatus")
    public Boolean updateSeckillStatus(SeckillStatusParameter seckillStatusParameter){
        try{
            adminService.updateSeckillStatus(seckillStatusParameter);
            return true;
        }catch (Exception e){
            return false;
        }
    }

    @GetMapping("/getAllPayOrders")
    public Integer getAllPayOrders(){
        return adminService.getAllPayOrders();
    }

    @GetMapping("/getAllRefundOrders")
    public Integer getAllRefundOrders(){
        return adminService.getAllRefundOrders();
    }

    @GetMapping("/getAllOrders")
    public List<ShopOrders> getAllOrders(){
        return adminService.getAllOrders();
    }
}
